<template>
  <div class="teacher-wrapper">
    <div class="teacher-header-wrapper">
      <div class="teacher-header-search-wrapper">
        <el-input class="teacher-header-search-input"
                  prefix-icon="el-icon-search"
                  v-model="teacherName"
                  placeholder="请输入老师名字进行搜索"></el-input>
        <el-button class="teacher-header-search-button"
                   icon="el-icon-search"
                   @click=""
                   type="primary">搜索</el-button>
      </div>
      <div class="teacher-header-operation-wrapper">
        <el-button class="teacher-header-add-button"
                   @click="showAddTeacherDialog"
                   type="primary"
                   icon="el-icon-plus">添加老师</el-button>
        <el-button class="teacher-header-delete-batch-button"
                   type="danger"
                   @click="deleteTeachersByBatch"
                   :disabled="this.multipleSelections.length === 0"
                   icon="el-icon-delete">批量删除</el-button>
      </div>
    </div>
    <div class="teacher-table-wrapper">
      <el-table class="teacher-table"
              :data="teachers"
              v-loading="loading"
              element-loading-text="拼命加载中"
              @selection-change="handleSelectionChange"
              stripe
              style="width: 100%">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="50">
        </el-table-column>
        <el-table-column
                prop="name"
                label="老师"
                width="90">
        </el-table-column>
        <el-table-column
                prop="gender"
                label="性别"
                width="60">
        </el-table-column>
        <el-table-column
                prop="department.name"
                label="所属学院"
                width="110">
        </el-table-column>
        <el-table-column
                prop="phone"
                label="联系电话"
                width="120">
        </el-table-column>
        <el-table-column
                prop="email"
                label="联系邮箱"
                width="170">
        </el-table-column>
        <el-table-column label="所授课程"
                         width="80">
          <template slot-scope="scope">
            <el-popover
                    placement="left"
                    title="课程列表"
                    @show="showPop(scope.row)"
                    trigger="hover">
              <el-tag style="display: block;width: 70%;margin-top: 5px"
                      type="success"
                      v-for="course in selectedCourses"
                      :key="course.id">{{course.name}}</el-tag>
              <el-button size="mini"
                         slot="reference"
                         type="success">查看</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="showEditTeacherDialog(scope.row)"
                       type="primary">编辑</el-button>
            <el-button type="danger"
                       @click="deleteTeacher(scope.row)"
                       size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog class="dialog"
               width="25%"
               :title="title"
               :visible.sync="dialogFormVisible">
      <div class="dialog-form-wrapper">
        <el-form class="dialog-form"
                 :rules="rules"
                 :model="teacher"
                 ref="teacherForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="老师名字:" prop="name">
                <el-input v-model="teacher.name"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入老师名字"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="性别:" prop="gender">
                <el-radio-group v-model="teacher.gender">
                  <el-radio-button label="男">男</el-radio-button>
                  <el-radio-button label="女">女</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="所属学院:" prop="departmentId">
                <el-select v-model="teacher.departmentId"
                           placeholder="请选择学院">
                  <el-option
                          v-for="item in departments"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="联系电话:" prop="phone">
                <el-input v-model="teacher.phone"
                          prefix-icon="el-icon-phone-outline"
                          placeholder="请输入联系电话"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="联系邮箱:" prop="email">
                <el-input v-model="teacher.email"
                          prefix-icon="el-icon-message"
                          placeholder="请输入联系邮箱"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogTeacher">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "EduTeachers",
    data() {
      return {
        teachers: [],
        departments: '',
        teacherName: '',
        multipleSelections: [],
        loading: false,
        title: '',
        dialogFormVisible: false,
        selectedCourses: [],
        courses: [],
        teacher: {
          id: null,
          name: '',
          departmentId: null,
          phone: '',
          email: ''
        },
        rules: {
          name: [{ required: true, message: '请输入老师名称', trigger: 'blur' }],
          gender: [{ required: true, message: '请选择老师性别', trigger: 'blur' }],
          departmentId: [{ required: true, message: '请选择所属学院', trigger: 'blur' }],
          phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
          email: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type:'email', required: true, message: '邮箱地址格式不正确', trigger: 'blur' },
          ]
        }
      }
    },
    mounted() {
      this.getTeachers()
      this.getDepartments()
    },
    methods: {
      showPop(data) {
        this.selectedCourses = []
        this.getCoursesByTeacher(data)
      },
      //  根据老师id获取课程
      getCoursesByTeacher(data) {
        this.getRequest('/edumanagement/teacher/course/' + data.id).then(resp => {
          if (resp) {
            this.selectedCourses = resp
          }
        })
      },
      //  获取所有老师
      getTeachers() {
        this.loading = true
        this.getRequest('/edumanagement/teacher/').then(resp => {
          if (resp) {
            this.teachers = resp
            this.loading = false
          }
        })
      },
      //  展示添加老师dialog
      showAddTeacherDialog() {
        this.title = '添加老师'
        this.teacher = {
          id: null,
          name: '',
          departmentId: null,
          phone: '',
          email: ''
        }
        this.dialogFormVisible = true
      },
      //  批量删除老师
      deleteTeachersByBatch() {
        this.$confirm('此操作将永久删除这['+ this.multipleSelections.length +']位老师, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelections.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/edumanagement/teacher/' + ids).then(resp => {
            if (resp) {
              this.getTeachers()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取被选中的老师
      handleSelectionChange(data) {
        this.multipleSelections = data
      },
      //  展示更新老师dialog
      showEditTeacherDialog(data) {
        this.title = '更新老师'
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(this.teacher, data)
        this.dialogFormVisible = true
      },
      //  删除老师
      deleteTeacher(data) {
        this.$confirm('此操作将永久删除['+ data.name +']老师, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/edumanagement/teacher/' + data.id).then(resp => {
            if (resp) {
              this.getTeachers()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  添加或更新老师
      dialogTeacher() {
        if (this.teacher.id) {
          //  更新老师
          this.$refs['teacherForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.putRequest('/edumanagement/teacher/', this.teacher).then(resp => {
                if (resp) {
                  this.getTeachers()
                }
              })
            }
          })
        } else {
          //  添加老师
          this.$refs['teacherForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.postRequest('/edumanagement/teacher/', this.teacher).then(resp => {
                if (resp) {
                  this.getTeachers()
                }
              })
            }
          })
        }
      },
      //  获取所有学院
      getDepartments() {
        this.getRequest('/edumanagement/teacher/department').then(resp => {
          if (resp) {
            this.departments = resp
          }
        })
      }
    }
  }
</script>

<style lang="less">
  .teacher-wrapper {
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .teacher-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .teacher-header-search-wrapper {
        display: flex;

        .teacher-header-search-input {
          margin-right: 10px;
        }

        .teacher-header-search-button {
          height: min-content;
        }

      }

      .teacher-header-operation-wrapper {

        .teacher-header-add-button {

        }

        .teacher-header-delete-batch-button {

        }
      }
    }

    .teacher-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;

      .teacher-table{

        .teacher-table-popover {

          .teacher-table-popover-tag {
            display: block;
            margin-right: 20px;
          }
        }
      }
    }
  }
</style>